AddCSLuaFile()
-------------------------------------------
------------- RPG:DM Utils ----------------
-------------------------------------------

function CreateVisualTimer(t, func, text, icon, ply)
	local TimerInfo = {
		t,
		text,
		icon
	}
	timer.Create(""..text.."", t, 1, func)
	net.Start("SendVisualTimer")
	net.WriteTable(TimerInfo)
	if(ply == nil)then
		net.Broadcast()
	else
		net.Send(ply)
	end
end

function GiveFlagTo(ent, flagtype)

	local flagmodel = ""
	
	if(ent:GetNWEntity("Flag") != nil and ent:GetNWEntity("Flag"):IsValid())then
		ent:GetNWEntity("Flag"):Remove()
	end
	if(flagtype == 2)then
		flagmodel = "models/devin/rpgdm/Arena_Flag_Yellow.mdl"
	end
	if(flagtype == 1)then
		flagmodel = "models/devin/rpgdm/Arena_Flag_Green.mdl"
	end
	if(flagtype == 3)then
		flagmodel = ""
	end
	
	local flag = ents.Create("prop_physics")
		flag:SetPos(ent:GetPos())
		flag:SetParent(ent)
		flag:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
		flag:SetModel(flagmodel)
		flag:AddEffects(EF_BONEMERGE)
	
	ent.Flag = flag
end

function CreateNPC(id, pos)
	local foundID = false
	local npc = ents.Create("npc_rpgdm")
	npc:SetCollisionBounds( Vector(-4,-4,0), Vector(4,4,64) ) 
	npc:SetPos(pos)
	for k, v in ipairs(NPCs)do
		if(NPCs[k].MobID == id)then
			print(NPCs[k].MobID)
			NPCs[k]:ApplyToEnt(npc)
			foundID = true
			break
		end
	end
	if(foundID == false)then
		print("Could not find NPC of ID#"..id..".")
		return false
	end
	npc:Spawn()
	npc:Activate()
	return npc
end

function SetEquipped(ent, tbl)
	if(ent.VisibleItems)then
		for q, w in pairs(ent.VisibleItems)do
			if(w and w:IsValid())then
				w:Remove()
			end
		end
	end

	if(ent:IsNPC() or ent:GetClass() == "npc_rpgdm")then
		for l, b in pairs(tbl)do
			for k, v in pairs(ITEM_LIST)do
				if( v[9] == b )then
					DisplayItem(b, ent)
				end
			end
		end
	end
	
	if(ent:IsPlayer())then
		for l, b in pairs(tbl)do
			for k, v in pairs(ITEM_LIST)do
				if( v[9] == b )then
					DisplayItem(b, ent)
					break;
				end
			end
		end
		--CalculateStats(ent)
	end
end

function DisplayItem(id, ent)

	if(ent.VisibleItems and ent.VisibleItems[ITEM_LIST[id][2]] != nil)then
		ent.VisibleItems[ITEM_LIST[id][2]]:Remove()
		ent.VisibleItems[ITEM_LIST[id][2]] = nil
	end
	
	local item = ents.Create("prop_physics")
	item:SetPos(ent:GetPos())
	item:SetParent(ent)
	item:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
	item:SetModel(ITEM_LIST[id][5])
	item:SetSkin(ITEM_LIST[id][6])
	if(ITEM_LIST[id][8] != "")then
		if(ITEM_LIST[id][2] == ITEM_HEAD)then
			ParticleEffectAttach( ITEM_LIST[id][8], PATTACH_POINT_FOLLOW, item, item:LookupAttachment("eyes"))
		end
		if(ITEM_LIST[id][2] == ITEM_SHOULDERS)then
			ParticleEffectAttach( ITEM_LIST[id][8], PATTACH_POINT_FOLLOW, item, item:LookupAttachment("lshoulder"))
			ParticleEffectAttach( ITEM_LIST[id][8], PATTACH_POINT_FOLLOW, item, item:LookupAttachment("rshoulder"))
		end
	end
	item:AddEffects(EF_BONEMERGE)
	
	if(!ent.VisibleItems)then
		ent.VisibleItems = {}
	end
	
	ent.VisibleItems[ITEM_LIST[id][2]] = item
	
	if(ent:IsPlayer())then
		print("Sending item table to "..ent:GetName()..".")
		net.Start("SendSelfItems")
		net.WriteTable(ent.tempEquippedItems)
		net.Send(ent)
	end
end

function CreateInteractible(pos)
	local npc = ents.Create("rpgdm_interactable")
	npc:SetPos(pos)
	npc:Spawn()
	npc:Activate()
end

function ClosestAllies(ent, target, num, dist)
	
end

function EnemiesInRange(ent, target, dist)
	local targets = ents.FindInSphere(target:GetPos(), dist)
	local closestTargets = {};
	for k, v in pairs(targets) do
		--print(""..v:EntIndex()..":"..target:EntIndex().."")
		if((v:GetClass() == "npc_rpgdm" or v:IsPlayer() or v:IsNPC()) and IsHostile(ent, v) and v:EntIndex() != target:EntIndex() and v:EntIndex() != ent:EntIndex())then
			table.insert(closestTargets, k, v)
		end
	end
	return closestTargets
end

function IsHostile(ent1, ent2)
	if(ent1:IsPlayer() and ent2:IsPlayer() and ent1:Team() != ent2:Team())then
		return true
	elseif(ent1.Faction != ent2.Faction)then
		return true
	else
		return false
	end
end
